﻿using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Yyd.AspNetCore
{
	public class CustomDateTimeConverter : JsonConverter<DateTime>
	{
		private readonly string Format;
		public CustomDateTimeConverter(string format)
		{
			Format = format;
		}

		//public override bool CanConvert(Type objectType)
		//{
		//	return typeof(DateTime).Equals(objectType);
		//}
		public override void Write(Utf8JsonWriter writer, DateTime date, JsonSerializerOptions options)
		{
			writer.WriteStringValue(date.ToString(Format));
		}
		public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
		{
			return DateTime.ParseExact(reader.GetString(), Format, null);
		}
	}
}
